const db = uniCloud.database();

exports.main = async (event, context) => {
  const { data = {} } = event;
  const { favorite_id, uid } = data;

  if (!uid) {
    return {
      code: 401,
      message: '未授权，请先登录',
      data: null
    };
  }

  if (!favorite_id) {
    return {
      code: 400,
      message: '收藏ID不能为空',
      data: null
    };
  }

  try {
    const res = await db.collection('food-favorites')
      .where({
        _id: favorite_id,
        user_id: uid
      })
      .remove();

    if (res.deleted === 0) {
      return {
        code: 404,
        message: '收藏不存在',
        data: null
      };
    }

    return {
      code: 200,
      message: '删除成功',
      data: null
    };
  } catch (err) {
    return {
      code: 500,
      message: err.message,
      data: null
    };
  }
};
